package pad.stand.com.haidiyun.www.widget;

import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import pad.stand.com.haidiyun.www.R;

public class MainBasePop {
    protected Context context;
    protected FrameLayout main_activity_view;
    protected View mLayout;

    public MainBasePop(Context context) {
        this.context = context;
        main_activity_view = (FrameLayout) ((Activity) context).findViewById(R.id.main_content);
    }

    private void initAnim() {
        LayoutTransition anim = new LayoutTransition();
        PropertyValuesHolder scaleInX = PropertyValuesHolder.ofFloat("scaleX", 0.8f, 1f);
        PropertyValuesHolder scaleInY = PropertyValuesHolder.ofFloat("scaleY", 0.8f, 1f);
        PropertyValuesHolder alphaIn = PropertyValuesHolder.ofFloat("alpha", 0.5f, 1f);
        PropertyValuesHolder scaleOutX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.8f);
        PropertyValuesHolder scaleOutY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.8f);
        PropertyValuesHolder alphaOut = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f);

        ObjectAnimator in = ObjectAnimator.ofPropertyValuesHolder((View) null, scaleInX, scaleInY, alphaIn);
        ObjectAnimator out = ObjectAnimator.ofPropertyValuesHolder((View) null, scaleOutX, scaleOutY, alphaOut);
        anim.setAnimator(LayoutTransition.APPEARING, in);
        anim.setAnimator(LayoutTransition.DISAPPEARING, out);
        anim.setDuration(500);
        anim.addTransitionListener(new LayoutTransition.TransitionListener() {
            @Override
            public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {

            }

            @Override
            public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
            }
        });
        main_activity_view.setLayoutTransition(anim);
    }

    protected synchronized void addView(int resource) {
        if (main_activity_view != null
                && mLayout == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mLayout = inflater.inflate(resource, main_activity_view, false);
            main_activity_view.addView(mLayout);
        }
    }

    protected synchronized void removeView() {
        if (main_activity_view != null
                && mLayout != null) {
            main_activity_view.removeView(mLayout);
            mLayout = null;
        }
    }

    public boolean isShowing() {
        if (mLayout != null) {
            return true;
        }
        return false;
    }
}
